package com.tritrees.easygridview.widget;

import java.util.HashMap;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;


public class DelEasyGridView extends EasyGridView
{

	public DelEasyGridView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	@Override
	protected void initAdapterData()
	{
		int num = 0;
		HashMap<String, Object> map;
		dataList.clear();

		if (data != null && data.size() != 0)
		{
			for (int i = 0; i < data.size(); i++)
			{
				dataList.add(data.get(i));
			}
			if (!isRemoved)
			{

				map = new HashMap<String, Object>();
				map.put(EasyGridView.IMAGE, bmp_del);
				dataList.add(map);
			}
			num = dataList.size();
			addBlankItem(num);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		if (!isRemoved && position == dataList.size() - blankItemCount - 1)
		{
			isRemoved = true;
			adapter.setRemoved(true);
			updateAdapter();
		} else if (isRemoved && position >= dataList.size() - blankItemCount)
		{// 当为删除状态时，单击空白区域，回到正常状态
			isRemoved = false;
			adapter.setRemoved(false);
			updateAdapter();
		} else
		{
			// 头像图标，在正常状态下，单击查询用户信息
			if (!isRemoved)
			{
				if (isChildItem)
				{
					if (handleGridViewWithChild != null)
					{
						handleGridViewWithChild.queryImage(childPosition,
								position);
					}
				} else
				{
					if (handleGridView != null)
					{
						handleGridView.queryImage(position);
					}
				}
			}
			// 在删除状态下，单击删除用户
			if (isRemoved)
			{
				if (isChildItem)
				{
					if (handleGridViewWithChild != null)
					{
						handleGridViewWithChild.delImage(childPosition,
								position);
					}
				} else
				{
					if (handleGridView != null)
					{
						handleGridView.delImage(position);
					}
				}

				if (data.size() == 0 || data == null)
				{// 当删除完最后一张图片后，此时没有图片数据，将不再是删除状态
					isRemoved = false;
					adapter.setRemoved(false);
				}
				updateAdapter();
			}

		}
	}

}
